<?php
/*
 *
 */
 
class catalog {
	public $categoryPointer;
	public $actualCategory;
	
	function dbLoadCategoryTree() {
		global $server;
		$sql = "select c.path from ".
		TABLE_CATEGORIES . " c";
		$result = $server->db->link->query($sql);
		$categoriesPaths=array();
		$load_check = ($result->num_rows > 0);
		if ($load_check) {
			while (list($idPath) = $result->fetch_row()) {
				array_push($categoriesPaths, $idPath);
			}
		}
		print_r($categoriesPaths);
		$this->categoryPointer=new categoryPointer();
		$this->categoryPointer->unserialize($categoriesPaths);
	}
}

//This is equivalent to node class but for sites node tree managing. It includes the site pointer as its information container variable.
class categoryPointer extends node {
	public $category; //This is a pointer to the site object form the node.
	
	//equivalent to the parent node function but for categoryPointers
	function unserialize($nodesTxt) {
		//$nodesTxt is an array of string node names splitted by the string delimiter "::"
		$stringDelimiter="::";
		foreach ($nodesTxt as $nodeTxt) {
			$nodePointer=$this;	//We point to the node. $this is a void node, acts only as a pointer to the node tree
			$nodeNamesPath=explode($stringDelimiter,$nodeTxt);
			foreach ($nodeNamesPath as $nodeName) {
				$index=$nodePointer->has_children($nodeName);
				if ($index >-1) {
					//we are navegating through a branch
					$nodePointer=$nodePointer->children[$index];
				}
				else {
					//New node because there is no such a node in the branch, we add it to the branch end
					$newNode=new categoryPointer();
					$newNode->name=$nodeName;
					$newNode->parentNode=$nodePointer;
					array_push($nodePointer->children, $newNode);
					$nodePointer=$newNode;	//$nodePointer points now to the last node added. If there are more iterations more nodes will be added
				}
			}
		}
	}
	//It loads the site information from all of the tree nodes
	function load_categories() {
		$nodePointer=$this; // Node pointer points at the index node.
		$copyPointer=unserialize(serialize($this));	//we create a copy for managing the node tree to manipulate without restrictions (look method remove_child)
		print_r($nodePointer);
		print_r($copyPointer);
		$startPointer=$copyPointer; // A pointer to the copy index
		$index=0;
		while (count($copyPointer->children) > 0) {
			while (count($copyPointer->children) > 0) {
				//we get to the last node of the branch
				$copyPointer=$copyPointer->children[0];
				$nodePointer=$nodePointer->children[0]; //we follow a parallel travel with the actual node tree pointer
			}
			$copyPointer->parentNode->remove_children(0);
			// We have to calculate what is the node index number for the original nodePointer and that is the total removed children less 1
			$nodeIndex=count($nodePointer->parentNode->children) - count($copyPointer->parentNode->children) -1;
			$nodePointer->parentNode->children[$nodeIndex]->load_category(); //We load the site object data
			$copyPointer=$startPointer; //we startby the beginning
			$nodePointer=$this; //the same for our actual node tree pointer
		}
	}
	//It load the site object and information for the actual site
	function load_category() {
		$this->category=new category();
		$this->category->id=$this->name;
		$this->category->db_load();
	}
	//It gets the site path name from the $host_name that is subdomain and domain
	function get_site_path($host_name){
		$stringDelimiter="::";
		$nodePointer=unserialize(serialize($this)); //we get a copy to work on it
		$nodeStart=$nodePointer;
		while (count($nodePointer->children) > 0) {
			$value=array();
			while (count($nodePointer->children) > 0) {
				$nodePointer=$nodePointer->children[0];
				array_push($value,$nodePointer->name);
				//we have created the list with the sites names to the end node
			}
			if ($nodePointer->site->is_host($host_name)) {
				//If there is coincidence to the $host_name we have found the siteNode and we can return the node list that is the path to the node
				return implode($stringDelimiter,$value);
			}
			else {
				// We haven't found it yet so we discard the end node to start the searching from the beginning
				$nodePointer->parentNode->remove_children(0); // $nodePointer siteNode is erased and nodePointer is null
				$nodePointer=$nodeStart; // We restart the search
			}
		}
		return false; //If there is no coincidence returns false
	}
}


class category extends myDbTb{
	public $id=0;
	public $name;
	public $parent_id;
	public $items=array();
	public $categories=array();

	function db_load() {
		$sql = "select c.* from ".
			TABLE_CATEGORIES . " c" .
			" where c.id=".$this->id .
			" ORDER BY c.sort_order";

		return $this->sql_request($sql);
	}
  
	/*
	* It loads the category items (products) from the database
	*/
	function db_load_items() {
		//, pd.products_name, pd.products_description, p.products_image, p.products_price, p.products_quantity, p.products_type, p.products_weight FROM 
		$sql= "SELECT p.id FROM " .
			TABLE_PRODUCTS . " p" .
			" WHERE p.status=1" .
			" AND p.category_id = ".$this->id.
			" ORDER BY p.sort_order";
			
		global $server;
		$result = $server->db->link->query($sql);
		$load_check = ($result->num_rows > 0);
		if ($load_check) {
			while (list($id) = $result->fetch_row()) {
				$my_item = new small_catalog_item();
				$my_item->id=$id;
				$my_item->db_load();
				array_push($this->items, $my_item);
			}
		}
		return $load_check;
	}
   
	 /*
	 * This function loads the subcategories of this category from the database
	 */
	function db_load_subcategories() {
		$sql= "select c.id from " .
			TABLE_CATEGORIES . " c" .
			" where c.parent_id=" . $this->id;
		global $server;
		$result = $server->db->link->query($sql);
		$load_check = ($result->num_rows > 0);
		if ($load_check) {
			while (list($id) = $result->fetch_row()) {
				$my_category= new category();
				$my_category->id=$id;
				$my_category->db_load(); 
				array_push($this->categories, $my_category);
			}
		}
		return $load_check;
	}
}

?>
